<?php

namespace App\Http\Controllers\Admin;

use App\Models\Banner;
use Illuminate\Http\Request;

class BannerController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('check_admin_login');
    }

    public function index()
    {
        $list = Banner::query()->orderBy('id','asc')->get();

        $items = [];
        if(!empty($list))
        {
            foreach ($list as $v)
            {
                $items[] = [
                    'id'        => $v->id,
                    'image'     => $v->image,
                    'url'       => $v->url,
                    'position'  => $v->position,
                    'is_show'   => $v->is_show,
                ];
            }
        }
        return $this->ok($items);
    }

    public function update(Request $request)
    {
        $id       = intval($request->get('id'));
        $url      = trim($request->get('url'));
        $image    = trim($request->get('image'));

        if(empty($id) || empty($url) || empty($image))
        {
            return $this->err(1,200,'','参数错误');
        }

        $banner = Banner::find($id);
        if(empty($banner))
        {
            return $this->err(1,200,'','banner不存在');
        }
        $banner->update([
            'url'         => $url,
            'image'       => $image,
            'updated_at'  => $this->time
        ]);
        return $this->ok([],'修改成功');
    }

    public function updateShow(Request $request)
    {
        $id       = intval($request->get('id'));
        $is_show  = intval($request->get('is_show',1));

        if(empty($id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $banner = Banner::find($id);
        if(empty($banner))
        {
            return $this->err(1,200,'','banner不存在');
        }
        $banner->update([
            'is_show'     => $is_show,
            'updated_at'  => $this->time
        ]);
        return $this->ok([],'设置成功');
    }
}
